package com.madou.dbtool.redis.node;


import com.madou.bean.BeanFactory;
import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.enums.DBEnum;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.common.messages.DBToolBundle;
import com.madou.dbtool.language.json.parser.JsonContentTypeParser;
import com.madou.dbtool.language.redis.parser.RedisContentTypeParser;
import com.madou.dbtool.redis.dialog.ConnectionDialog;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.relationship.common.BaseNonRelationNodeParm;
import com.madou.dbtool.relationship.node.menu.NodeMenuFactory;
import com.madou.dbtool.ui.tree.DataSourceNode;
import com.madou.dbtool.utils.ConfigManager;
import com.madou.inters.util.MessageUtil;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author miukoo
 * Redis数据源信息节点
 */
@HideClass("redisDataSourceNode")
public class RedisDataSourceNode extends BaseNonRelationNodeParm {
    RedisContentTypeParser instance1 = RedisContentTypeParser.INSTANCE;
    JsonContentTypeParser instance2 = JsonContentTypeParser.INSTANCE;
    private static List<JMenuItem> DB_MENU = new ArrayList<>();
    static {
        DB_MENU.addAll(DEFAULT_NODE_MENU);
        DB_MENU.add(createMenu(DBToolBundle.message("ds.tree.datasource.add"), 65,65, AllIcons.MENU.ADD));
        DB_MENU.add(createMenu(DBToolBundle.message("ds.tree.datasource.edit"), 69,69, AllIcons.MENU.EDIT));
        DB_MENU.add(createMenu(DBToolBundle.message("ds.tree.datasource.del"), 68,68, AllIcons.MENU.REMOVE));
    }
    // 数据库类型
    DBEnum type = DBEnum.REDIS;
    // 资源管理器
    RedisSourceManager redisSourceManager;
    // 显示名称
    String title;
    RedisServerGroupNode cacheNode;

    public RedisDataSourceNode(DataSourceNode parent, RedisSourceManager redisSourceManager, String title) {
        super(parent);
        this.redisSourceManager = redisSourceManager;
        this.title = title;
    }

    @Override
    public DataSourceNode getChildren(DataSourceNode parent, int index) {
        if(cacheNode==null){
            cacheNode = new RedisServerGroupNode(this,redisSourceManager);
        }
        return cacheNode;
    }

    @Override
    public String getText() {
        return title;
    }

    @Override
    public Icon getIcon() {
        if(open){
            return type.getOpenIcon();
        }
        return type.getCloseIcon();
    }

    @Override
    public int countChild() {
        return 1;
    }

    @Override
    public boolean hasChild() {
        return true;
    }

    /**
     * 子元素右键菜单
     * @return
     */
    @Override
    public List<JMenuItem> rightMenu(){
        List temp = new ArrayList();
        for (JMenuItem dbMenu : DB_MENU) {
            temp.add(NodeMenuFactory.copyMenuItem(dbMenu));
        }
        return temp;
    }

    /**
     * 子元素右键菜单点击
     * @return
     */
    @Override
    public void clickMenu(JTree jTree, TreePath treePath, JMenuItem jMenuItem){
        super.clickMenu(jTree,treePath,jMenuItem);
        if(isMenuCode(jMenuItem,65)){// 新增
            ConnectionDialog.actionRedisConnect(UUID.randomUUID().toString(),DBToolBundle.message("connect.button.add"),null);
        }else if(isMenuCode(jMenuItem,69)){// 编辑
            HashMap treeNode = (HashMap) ConfigManager.getInstance().getTreeNode(type);
            HashMap value = (HashMap) treeNode.get(getId());
            if(value==null&&treeNode.size()==1){
                String next = (String) treeNode.keySet().iterator().next();
                setId(next);
                value = (HashMap) treeNode.get(next);
            }
            ConnectionDialog.actionRedisConnect(getId(),DBToolBundle.message("connect.button.edit"),value);
        }else if(isMenuCode(jMenuItem,68)){// 删除
            int tip = BeanFactory.getInstance().getBean(MessageUtil.class).showConfirmMessage("确定要删除" + getText() + "数据源?", "Tip");
            if(tip == JOptionPane.YES_OPTION){
                HashMap treeNode = (HashMap) ConfigManager.getInstance().getTreeNode(type);
                treeNode.remove(getId());
                ConfigManager.getInstance().setTreeNode(type,treeNode);
            }
        }
    }
}
